In [1]:
import string
import scipy
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
import sys
import glob
import cPickle
import skimage
sys.path.append(os.path.abspath("C:\Users\Scherer Lab E\Documents\GitHub\Python_Data_Analysis"))
import common_functions as cf
import pims
import passing_event_functions as pef
import trackpy_helper_functions as tphf
import optical_binding_of_driven_particles as obdp
C:\Users\Scherer Lab E\Anaconda2\envs\170112\lib\site-packages\skimage\viewer\utils\core.py:10: UserWarning: Recommended matplotlib backend is `Agg` for full skimage.viewer functionality.
  warn("Recommended matplotlib backend is `Agg` for full "

Load the database

In [2]:
store = pd.HDFStore("K:\Pat's_Projects\ParticleTrajectoryData\passing_event_data_trackpy_refined_extra.h5", mode='r')
keys = store.index['key']

Create a giant dataframe that has all the data in it. Note that track id names are reduntact between different experiments.

In [3]:
df_list = []
for key in keys:
    df = store.get(key)
    df_list.append(df)

Distribution of Particle Deviation from the Mean Radius

When a passing event occurs look at how far each particle is from the mean radius of the trap. This is separated into two histograms, one for the particle with the smaller deviation from the mean radius and one for the particle with the larger deviation from the mean radius. When separating with these two conditions the characteristics of each histogram become apparent. Particles that have a smaller deviation from the average radius are distributed in a much sharper around 0 deviation. While particles a that are further from the average radius are distributed bimodally. This may illustrate the idea that during a passing event it is essential for one of the two particles to be in the trap for a passing to occur.

In [4]:
df_all = pd.concat(df_list)
df_pass = df_all[df_all['passing_event'] ==True]
output = plt.hist(df_pass.drop_duplicates(['frame', 'track id']).r, bins=100)
plt.ylabel('Counts')
plt.xlabel('r (pixels)')
plt.show()

df_pass['dist_avg_r'] = df_pass['r'] - df_pass['r_avg']
output = plt.hist(abs(df_pass.drop_duplicates(['frame', 'track id']).dist_avg_r), bins=100)
plt.ylabel('Counts')
plt.xlabel('abs(r_avg - r) (pixels)')
plt.show()

df_pass['dist_avg_r_other_part'] = df_pass['nn_r'] - df_pass['r_avg']
df_case_1 = df_pass[abs(df_pass.dist_avg_r) > abs(df_pass.dist_avg_r_other_part)]
df_case_2 = df_pass[abs(df_pass.dist_avg_r) < abs(df_pass.dist_avg_r_other_part)]
output_1 = plt.hist(df_case_1['dist_avg_r'], bins=100, label='particle with\n larger abs(r_avg - r)', range=[-30, 30], normed=True, alpha=0.5)
output_2 = plt.hist(df_case_2['dist_avg_r'], bins=100, label='particle with\n smaller abs(r_avg - r)', range=[-30, 30], normed=True, alpha=0.5)
plt.ylabel('Probabilty Density')
plt.xlabel('r_avg - r (pixels)')
plt.legend()
plt.show()
C:\Users\Scherer Lab E\Anaconda2\envs\170112\lib\site-packages\ipykernel\__main__.py:8: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
C:\Users\Scherer Lab E\Anaconda2\envs\170112\lib\site-packages\ipykernel\__main__.py:14: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
In [5]:
f = open("C:\Users\Scherer Lab E\Box Sync\Passing\Figures and Data\Data\pass_event_r_deviation_closer_vs_further.pkl", mode='w')
cPickle.dump([df_case_1['dist_avg_r'], df_case_2['dist_avg_r']], f)
f.close()

Plot which particle stayed on trap in successful passing event

In [5]:
part_behind_r_dev = []
part_ahead_r_dev = []

for df in df_list:
    last_pass_success = df[df.last_pass_event==True]
    last_pass_success = last_pass_success[last_pass_success.del_theta < 0]
    part_behind_r_dev.append(last_pass_success.dist_avg_r_other_part)
    part_ahead_r_dev.append(last_pass_success.dist_avg_r)
In [6]:
part_behind_r_dev_full = pd.concat(part_behind_r_dev)
part_ahead_r_dev_full = pd.concat(part_ahead_r_dev)

plt.hist(part_behind_r_dev_full, bins=50,range=[-20,20], alpha=0.5)
plt.hist(part_ahead_r_dev_full, bins=50,range=[-20,20], alpha=0.5)
Out[6]:
(array([   5.,    4.,    6.,   18.,   11.,   15.,   21.,   21.,   20.,
          41.,   34.,   48.,   48.,   75.,   69.,   68.,   69.,  103.,
          99.,   88.,   96.,  119.,  116.,  132.,  136.,  157.,  144.,
         126.,  125.,  112.,  114.,  104.,   94.,   97.,   60.,   63.,
          60.,   78.,   63.,   41.,   34.,   34.,   26.,   21.,   12.,
          14.,   12.,    2.,    1.,   10.]),
 array([-20. , -19.2, -18.4, -17.6, -16.8, -16. , -15.2, -14.4, -13.6,
        -12.8, -12. , -11.2, -10.4,  -9.6,  -8.8,  -8. ,  -7.2,  -6.4,
         -5.6,  -4.8,  -4. ,  -3.2,  -2.4,  -1.6,  -0.8,   0. ,   0.8,
          1.6,   2.4,   3.2,   4. ,   4.8,   5.6,   6.4,   7.2,   8. ,
          8.8,   9.6,  10.4,  11.2,  12. ,  12.8,  13.6,  14.4,  15.2,
         16. ,  16.8,  17.6,  18.4,  19.2,  20. ]),
 <a list of 50 Patch objects>)
In [7]:
f = open("C:\Users\Scherer Lab E\Box Sync\Passing\Figures and Data\Data\pass_event_r_deviation.pkl", mode='w')
cPickle.dump([part_behind_r_dev_full, part_ahead_r_dev_full], f)
f.close()
In [ ]:
test = df_list[-1]
iter_df = test[test.last_pass_event==True].drop(['x pos', 'y pos', 'theta_nn_id', 'theta_nn_dist', 'theta_nn_num', 'dist_avg_r', 'dist_avg_r_other_part'], axis=1)

print iter_df
for key,frame in iter_df.frame.iteritems():
    selection = (test.frame < frame) & (test.pass_success == True)
    print test[selection].drop(['x pos', 'y pos', 'theta_nn_id', 'theta_nn_dist', 'theta_nn_num', 'dist_avg_r', 'dist_avg_r_other_part'], axis=1)
In [5]:
def plot_rel_r_theta_particle_in_front_start_end(df, min_r):
    
    df_events = df[(df.pass_event_start == True) & (df.del_theta < 0) & (df.del_r < min_r)]
    for idx, series in df_events.iterrows():
        lower_frame = series.frame
        upper_frame = series.pass_frame_end
        df_plot = df.query('@lower_frame <= frame <= @upper_frame')
        df_plot = df_plot[df_plot['track id'] == series['track id']]
        df_plot = df_plot[df_plot.theta_nn_id == series.theta_nn_id]
        plt.plot(df_plot.del_theta, df_plot.del_r, lw=0.4)
In [6]:
def find_if_pass_was_successful(df):
    """Label the passing events where a pass was successful. Requires
    a DataFrame where the pass_event_start and pass_event_end and 
    frame_start and frame_end are available.
    """
    df_copy = df.copy()
    df_copy['pass_success'] = np.nan
    df_start = df[df.pass_event_start==True]
    grouped = df_start.groupby(['frame', 'pair_id'])
    for frame, pair_id in grouped.groups.keys():
        single_part_start = df[(df.frame==frame) & (df.pair_id==pair_id) & (df.del_theta < 0)]
        if len(single_part_start)==0 or single_part_start.pass_frame_end.values[0] == np.nan:
            continue
        frame_end = single_part_start.pass_frame_end.values[0]
        track_id = single_part_start['track id'].values[0]
        single_part_end = df[(df.frame==frame_end) & (df.pair_id==pair_id) & (df['track id'] == track_id)]
        selection_crit = (df_copy.frame==frame) & (df_copy.pair_id==pair_id)
        if len(single_part_end) == 0:
            continue
        if np.sign(single_part_start.del_theta.values[0]) != np.sign(single_part_end.del_theta.values[0]):
            df_copy.loc[selection_crit, 'pass_success'] = True
        else:
            df_copy.loc[selection_crit, 'pass_success'] = False
    return df_copy
In [95]:
def last_passing_event(df):
    """Find the last time that del_theta sign switches for only successful
    passing events.
    """
    
    df_copy = df.copy()
    df_copy['last_pass_event'] = False
    df_success = df_copy[(df_copy['pass_success']==True)]
    grouped = df_success.groupby(['frame', 'pair_id'])
    for frame, pair_id in grouped.groups.keys():

        pass_df = df_copy[(df_copy['frame']==frame) & (df_copy['pair_id']==pair_id)]
        pass_frame_end = pass_df['pass_frame_end'].iloc[0]
        passing_frames = df_copy.query('@frame <= frame <= @pass_frame_end')
        passing_frames = passing_frames[passing_frames.pair_id==pair_id]
        passing_frames = passing_frames[passing_frames.passing_event==True]
        passing_frames = passing_frames.sort_values('frame')

        last_pass_frame = passing_frames['frame'].iloc[-1]
        selection_crit = (df_copy.frame==last_pass_frame) & (df_copy.pair_id==pair_id)
        df_copy.loc[selection_crit, 'last_pass_event'] = True

    return df_copy
In [101]:
store.close()